package com.platform.cdcs.fragment.detail;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.platform.cdcs.R;
import com.platform.cdcs.model.BaseObjResponse;
import com.platform.cdcs.model.ProductList;
import com.platform.cdcs.tool.Constant;
import com.platform.cdcs.tool.FragmentUtil;
import com.platform.cdcs.tool.ViewTool;
import com.trueway.app.uilib.fragment.BaseFragment;
import com.trueway.app.uilib.tool.Utils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by holytang on 2017/11/20.
 */
public class ProductInfoFragment extends BaseFragment {
    private View rootView;
    private ProductList.ProductItem item;
    private int type;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        item = (ProductList.ProductItem) getArguments().getSerializable("model");
        type = getArguments().getInt("type");
    }

    @Override
    public void initView(View view) {
        initLoadImg(view.findViewById(R.id.load));
        getToolBar().setNavigationIcon(R.mipmap.icon_back);
        getToolBar().setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        setTitle("产品详情");
        rootView = view;
        rootView.findViewById(R.id.text).setVisibility(View.GONE);
        requestDetail();
    }

    @Override
    public int layoutId() {
        return R.layout.five_layout;
    }

    private void setData(final ProductList.ProductItem item) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        LinearLayout root1 = (LinearLayout) rootView.findViewById(R.id.button1);
        TextView[] views = ViewTool.setFourItemOnly(inflater, root1, new String[]{item.getMaterialName(), item.getLicense_no()});
        views[0].setTextSize(18);
        views[0].getPaint().setFakeBoldText(true);
        ViewTool.setSixItem(inflater, root1, new String[]{"产品型号", "产品线", "销售单位"}, new String[]{item.getMaterialNumber(), item.getSubBU(), item.getSaleUnit()});
        LinearLayout root2 = (LinearLayout) rootView.findViewById(R.id.button2);
        ViewTool.setSixItem(inflater, root2, new String[]{"批号", "生产日期", "失效期"}, new String[]{item.getBatchNumber(), item.getDom_date(), item.getExpDate()});
        LinearLayout root3 = (LinearLayout) rootView.findViewById(R.id.button3);
        if (!TextUtils.isEmpty(item.getSerialNumber())) {
            ViewTool.setFourItem(inflater, root3, new String[]{"序列号", "PKG"}, new String[]{item.getSerialNumber(), item.getPkg()});
        } else {
            root3.setVisibility(View.GONE);
        }
        LinearLayout root4 = (LinearLayout) rootView.findViewById(R.id.button4);
        ViewTool.setSixItem(inflater, root4, new String[]{"状态", "库位", "数量"}, new String[]{item.getStatus(), item.getWhName(), item.getQty()});
        LinearLayout root5 = (LinearLayout) rootView.findViewById(R.id.button5);
//        View ticketView = inflater.inflate(R.layout.left_right, null);
//        TextView ticketLV = (TextView) ticketView.findViewById(R.id.title);
//        ticketLV.setText("发票");
//
//        TextView ticketRV = (TextView) ticketView.findViewById(R.id.text);
//        ticketRV.setText("");
//        ticketRV.setCompoundDrawablePadding(Utils.convertDIP2PX(getContext(), 10));
//        ticketRV.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.icon_right, 0);
//        ticketView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                //TODO 产品发票
//                Bundle bundle = new Bundle();
//                bundle.putString("id", item.getProductId());
//                bundle.putString("serialNumber", item.getSerialNumber());
//                FragmentUtil.navigateToInNewActivity(getActivity(), ProductTicketFragment.class, bundle);
//            }
//        });
//        root5.addView(ticketView);

        View lineView;
        LinearLayout.LayoutParams lineLP;
//        View lineView = new View(getContext());
//        LinearLayout.LayoutParams lineLP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
//        lineView.setLayoutParams(lineLP);
//        lineView.setBackgroundColor(getResources().getColor(R.color.gray_text));
//        root5.addView(lineView);

        if (type != 2) {
            View inoutView = inflater.inflate(R.layout.left_right, null);
            inoutView.setBackground(null);
            TextView inoutLV = (TextView) inoutView.findViewById(R.id.title);
            if (type == 0) {
                inoutLV.setText("产品出入库单");
            } else {
                inoutLV.setText("产品归属");
            }
            TextView inoutRV = (TextView) inoutView.findViewById(R.id.text);
            inoutRV.setText("");
            inoutRV.setCompoundDrawablePadding(Utils.convertDIP2PX(getContext(), 10));
            inoutRV.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.icon_right, 0);
            inoutView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("model", item);
                    if (type == 0) {
                        bundle.putInt("type", 0);
                        FragmentUtil.navigateToInNewActivity(getActivity(), InOutTicketFragment.class, bundle);
                    } else {
                        FragmentUtil.navigateToInNewActivity(getActivity(), ProductBelongFragment.class, bundle);
                    }
                }
            });
            root5.addView(inoutView);
            lineView = new View(getContext());
            lineLP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
            lineView.setLayoutParams(lineLP);
            lineView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.gray_text));
            root5.addView(lineView);
        }
        if (!TextUtils.isEmpty(item.getSerialNumber())) {
            View productLineView = inflater.inflate(R.layout.left_right, null);
            productLineView.setBackground(null);
            TextView lineLV = (TextView) productLineView.findViewById(R.id.title);
            lineLV.setText("产品轨迹");
            TextView lineRV = (TextView) productLineView.findViewById(R.id.text);
            lineRV.setText("");
            lineRV.setCompoundDrawablePadding(Utils.convertDIP2PX(getContext(), 10));
            lineRV.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.icon_right, 0);
            productLineView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putString("serialNumber", item.getSerialNumber());
                    FragmentUtil.navigateToInNewActivity(getActivity(), LineTicketFragment.class, bundle);
                }
            });
            root5.addView(productLineView);
        }
        if (root5.getChildCount() == 0) {
            root5.setVisibility(View.GONE);
        }
    }

    private void requestDetail() {
        if ((TextUtils.isEmpty(item.getProductId()) && TextUtils.isEmpty(item.getSerialNumber()))) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            LinearLayout root1 = (LinearLayout) rootView.findViewById(R.id.button1);
            TextView[] views = ViewTool.setFourItemOnly(inflater, root1, new String[]{item.getItemName(), ""});
            views[0].setTextSize(18);
            views[0].getPaint().setFakeBoldText(true);
            String subBu = item.getSubBUCode();
            if (TextUtils.isEmpty(subBu)) {
                subBu = item.getSubbu();
            }
            ViewTool.setSixItem(inflater, root1, new String[]{"产品型号", "产品线", "销售单位"}, new String[]{item.getItemCode(), subBu, item.getUom()});
            LinearLayout root2 = (LinearLayout) rootView.findViewById(R.id.button2);
            ViewTool.setSixItem(inflater, root2, new String[]{"批号", "生产日期", "失效期"}, new String[]{item.getLots(), item.getDom_date(), item.getExpDate()});
            LinearLayout root3 = (LinearLayout) rootView.findViewById(R.id.button3);
            root3.setVisibility(View.GONE);
            LinearLayout root4 = (LinearLayout) rootView.findViewById(R.id.button4);
            ViewTool.setSixItem(inflater, root4, new String[]{"状态", "库位", "数量"}, new String[]{item.getStatus(), item.getWhName(), item.getQty()});
            LinearLayout root5 = (LinearLayout) rootView.findViewById(R.id.button5);
            root5.setVisibility(View.GONE);
            return;
        }
        showLoadImg();
        Map<String, String> param = new HashMap<>();
        param.put("productId", item.getProductId());
        param.put("serialNumber", item.getSerialNumber());
        param.put("distCode", TextUtils.isEmpty(item.getDistCode()) ? "" : item.getDistCode());
        param.put("distName", TextUtils.isEmpty(item.getDistName()) ? "" : item.getDistName());
        getHttpClient().post().url(Constant.PRODUCT_DETAIL).params(Constant.makeParam(param)).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int i) {
                dismissLoadImg();
                Utils.showToast(getContext(), R.string.server_error);
            }

            @Override
            public void onResponse(String s, int i) {
                try {
                    dismissLoadImg();
                    if (getContext() == null) {
                        return;
                    }
                    Type type = new TypeToken<ProResult>() {
                    }.getType();
                    ProResult response = new Gson().fromJson(s, type);
                    if ("1".equals(response.getResult().getCode())) {
                        setData(response.getResult());
                    } else {
                        Utils.showToast(getContext(), "获取产品失败");
                    }
                } catch (Exception e) {
                    Utils.showToast(getContext(), "解析数据失败");
                }
            }
        });
    }

    private class ProResult {
        private ProductList.ProductItem result;

        public ProductList.ProductItem getResult() {
            return result;
        }

        public void setResult(ProductList.ProductItem result) {
            this.result = result;
        }

    }
}
